<template>
  <view class="container">
    <!-- 自定义头部，如果需要的话 -->
    <!-- <view class="custom-header">
      <text class="page-title">健康计算器</text>
    </view> -->

    <view class="bmi-card">
      <text class="card-title">身体质量指数 (BMI) 计算</text>
      <view class="input-group">
        <text class="label">身高 (cm):</text>
        <input class="input-field" type="digit" v-model="height" placeholder="请输入身高" />
      </view>
      <view class="input-group">
        <text class="label">体重 (kg):</text>
        <input class="input-field" type="digit" v-model="weight" placeholder="请输入体重" />
      </view>
      <view class="button-group">
        <button class="calculate-btn" @tap="calculateBMI">计算 BMI</button>
        <button class="clear-btn" @tap="clearFields">清空</button>
      </view>

      <view class="result-area" v-if="bmi">
        <text class="result-title">您的 BMI:</text>
        <text class="bmi-value">{{ bmi.toFixed(2) }}</text>
        <text :class="['status-text', bmiStatusClass]">{{ bmiStatus }}</text>
      </view>

      <view class="tips-area">
        <text class="tips-title">BMI 指数参考:</text>
        <view class="tips-list">
          <text>• 18.5 以下: 体重过轻</text>
          <text>• 18.5 - 24: 正常范围</text>
          <text>• 24 - 28: 体重超重</text>
          <text>• 28 以上: 肥胖</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      height: '', // 身高 (cm)
      weight: '', // 体重 (kg)
      bmi: null, // BMI结果
      bmiStatus: '', // BMI健康状况
      bmiStatusClass: '', // BMI健康状况对应的样式类
    };
  },
  methods: {
    calculateBMI() {
      if (!this.height || !this.weight) {
        uni.showToast({ title: '请输入身高和体重', icon: 'none' });
        return;
      }
      const h = parseFloat(this.height) / 100; // 将厘米转换为米
      const w = parseFloat(this.weight);

      if (isNaN(h) || isNaN(w) || h <= 0 || w <= 0) {
        uni.showToast({ title: '请输入有效的身高和体重', icon: 'none' });
        return;
      }

      this.bmi = w / (h * h);
      this.getBmiStatus(this.bmi);
    },
    getBmiStatus(bmi) {
      if (bmi < 18.5) {
        this.bmiStatus = '体重过轻';
        this.bmiStatusClass = 'status-underweight';
      } else if (bmi >= 18.5 && bmi < 24) {
        this.bmiStatus = '正常范围';
        this.bmiStatusClass = 'status-normal';
      } else if (bmi >= 24 && bmi < 28) {
        this.bmiStatus = '体重超重';
        this.bmiStatusClass = 'status-overweight';
      } else {
        this.bmiStatus = '肥胖';
        this.bmiStatusClass = 'status-obese';
      }
    },
    clearFields() {
      this.height = '';
      this.weight = '';
      this.bmi = null;
      this.bmiStatus = '';
      this.bmiStatusClass = '';
    },
  },
};
</script>

<style scoped>
.container {
  padding: 0;
  background-color: #f5f8fa;
  min-height: 100vh;
  box-sizing: border-box;
}

.custom-header {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100rpx; /* 固定高度 */
  background-color: #fff;
  border-bottom: 1rpx solid #eee;
  margin-top: var(--window-top); /* 适配原生导航栏高度 */
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.bmi-card {
  background-color: #fff;
  border-radius: 16rpx;
  margin: 30rpx;
  padding: 40rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.card-title {
  font-size: 38rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 40rpx;
  display: block;
  text-align: center;
}

.input-group {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.label {
  font-size: 30rpx;
  color: #666;
  width: 150rpx;
  flex-shrink: 0;
}

.input-field {
  flex: 1;
  height: 90rpx;
  border: 1rpx solid #dcdfe6;
  border-radius: 16rpx;
  padding: 0 30rpx;
  font-size: 30rpx;
  box-sizing: border-box;
}

.button-group {
  display: flex;
  justify-content: space-around;
  margin-top: 50rpx;
  margin-bottom: 40rpx;
}

.calculate-btn, .clear-btn {
  width: 45%;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 32rpx;
  border-radius: 45rpx;
  margin: 0;
  padding: 0;
  color: #fff;
}

.calculate-btn {
  background: linear-gradient(135deg, #007bff, #0056b3); /* 渐变蓝色 */
  box-shadow: 0 8rpx 16rpx rgba(0, 123, 255, 0.3);
}

.clear-btn {
  background-color: #f0f0f0;
  color: #666;
}

.result-area {
  background-color: #e6f7ff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-top: 30rpx;
  text-align: center;
  border: 1rpx solid #a8e3ff;
}

.result-title {
  font-size: 32rpx;
  color: #007bff;
  font-weight: bold;
  margin-bottom: 15rpx;
  display: block;
}

.bmi-value {
  font-size: 72rpx;
  font-weight: bold;
  color: #007bff;
  margin-bottom: 20rpx;
  display: block;
}

.status-text {
  font-size: 36rpx;
  font-weight: bold;
}

.status-underweight { color: #f0ad4e; /* 橙色 */ }
.status-normal { color: #5cb85c; /* 绿色 */ }
.status-overweight { color: #f0ad4e; /* 橙色 */ }
.status-obese { color: #d9534f; /* 红色 */ }

.tips-area {
  margin-top: 50rpx;
  padding-top: 30rpx;
  border-top: 1rpx dashed #eee;
}

.tips-title {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.tips-list text {
  font-size: 26rpx;
  color: #666;
  line-height: 1.8;
  display: block;
}
</style>
